package com.xmw.concurrent.exechanger;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExchangerTest {
    private static final Exchanger<String> exgr = new Exchanger<>();

    private static final ExecutorService theadPool = Executors.newFixedThreadPool(2);

    public static void main(String[] args) {
        theadPool.execute(() -> {
            try {
                String A = "银行流水A";
                exgr.exchange(A);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        theadPool.execute(() -> {
            try {
                String B = "银行流水B";
                String A = exgr.exchange(B);
                System.out.println("A和B数据是否一致: " + A.equals(B) + ", A录入的是: " + A + ", B录入的是: " + B);
                // A和B数据是否一致: false, A录入的是: 银行流水A, B录入的是: 银行流水B
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        theadPool.shutdown();
    }
}
